
package com.qdu.dao.impl;

import com.qdu.entity.Admin;
import java.util.List;
import com.qdu.dao.AdminDao;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class AdminDaoImpl extends BaseDaoImpl implements AdminDao{

    @Override
    public int insert(Admin a) {
        
        return executeUpdate("insert into Admin values(?,?,?)"
                ,a.getAno()
                ,a.getAname()
                ,a.getApwd()
        );
    }

    @Override
    public int update(Admin a) {
        return executeUpdate("update Admin set Aname=?,Apwd=? where Ano=?"
                ,a.getAname()
                ,a.getApwd()
                ,a.getAno());
    }

    @Override
    public List<Admin> getAll() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Admin> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Admin");
            rs = ps.executeQuery();
            while(rs.next()){
                list.add(new Admin(rs.getInt(1),rs.getString(2),rs.getString(3)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(null, ps, con);
        }
        return list;
    }

    @Override
    public int deleteByAno(int Ano) {
        return executeUpdate("delete Admin where Ano=?",Ano);
    }

    @Override
    public Admin getOneByAno(int Ano) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
   

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Admin where Ano=?");
            ps.setInt(1, Ano);
            rs = ps.executeQuery();
            if(rs.next()){
                return new Admin(rs.getInt(1),rs.getString(2),rs.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return null;
    }

    @Override
    public int modifyApwd(Admin a) {
         return executeUpdate("update Admin set Apwd=? where Ano=?",a.getAno(), a.getApwd()); 
    }
}
